<?php
	include ('../defines.php');

	FunctionsHelper::includeFiles(
		array(
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'players.php',
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'countries.php',
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'users.php'
		)
	);
	
	$users = new Users();
	if (!$users->isLoggedUser())
		header('Location:' . ADMINISTRATOR_VIEW_PATH . 'login.php');

	$user = $users->getLoggedUser();
	$players = new Players();
	
	if (!empty($_POST)){
		$arrPlayer = array();
		unset($_POST['sbmSubmit']);

		foreach ($_POST as $key => $value)
			$arrPlayer[$key] = $value;

		if ($_POST['action'] != 'a'){
			if ($players->updatePlayer($arrPlayer))
				header('Location:' . ADMINISTRATOR_VIEW_PATH . 'players.php');
		}else if ($_POST['action'] == 'a'){
			if ($players->createPlayer($arrPlayer))
				header('Location:' . ADMINISTRATOR_VIEW_PATH . 'players.php');
		}		
	}

	$checkedMale = 'checked="checked"';
	$checkedFemale = '';
	
	if ($_GET['action'] != 'a'){
		$player = $players->getPlayer(
			array(
				'player_id' => $_GET['id']
			)
		);
		
		if ($player->getSex() == 'F'){
			$checkedMale = '';
			$checkedFemale = 'checked="checked"';
		}
	}else{
		$player = $players->getPlayer();
	}

	$countries = new Countries();
	$arrCountries = array();
	$arrCountries = $countries->getAllActive();

	FunctionsHelper::includeTop();
?>
<div class="withBorder" id="main">
	<script type="text/javascript">
		$(function(){
			$('#back').click(function(){
				document.location.href='<?=ADMINISTRATOR_VIEW_PATH;?>players.php';
			});
		});
	</script>
	<div class="mainABM">
		<div class="ABMtitle"><h1>ADMINISTRADOR DE JUGADORES</h1></div>
		<form id='frmPlayer' class='form' method='POST' enctype="multipart/form-data">
			<input type='hidden' name='player_id' id='player_id' value='<?= $player->getPlayerId(); ?>' />
			<input type='hidden' name='action' id='action' value='<?= $_GET['action']; ?>' />
			<div class="label">Nombre:</div><input type='text' value='<?= $player->getName();?>' class='required tinyInput' name='name' id='name' />
			<div class="label">Apellido:</div><input type='text' value='<?= $player->getSurname();?>' class='required tinyInput' name='surname' id='surname' />
			<br />
			<div class="label">Pa&iacute;s:</div> 
			<select id='country_id' name='country_id'>
			<?php
				foreach($arrCountries as $country){
			?>
				<option value='<?= $country->getCountryId(); ?>' <?= ($player->getCountryId() == $country->getCountryId()) ? 'selected="selected"' : ''; ?> ><?= $country->getName(); ?></option>
			<?php
				}
			?>
			</select>
			<br />
			<div class="label">Nacimiento:</div><input type='text' value='<?= $player->getBirthdate();?>' class='required tinyInput dateP' name='birthdate' id='birthdate' />
			<div class="label">Puntos:</div><input type='text' value='<?= $player->getPoints();?>' class='required tinyInput justNumeric' name='points' id='points' />
			<div class="label">Ranking:</div><input type='text' value='<?= $player->getRanking();?>' class='required tinyInput justNumeric' name='ranking' id='ranking' />
			<div class="label">Sexo:</div><input type='radio' value='M' class='required' name='sex' id='sexM' <?=$checkedMale;?> />M <input type='radio' value='F' class='required' name='sex' id='sexF' <?=$checkedFemale;?> />F									
			<br />
			<br />
			<input type="checkbox" name='active' id='active' value='1' <? echo $player->getActive() == 1 ? 'checked = "checked"' : ''; ?> /><label for="active">Activo</label>
			<br />
			<div id="button"><input type="submit" name='sbmSubmit' id='sbmSubmit' value='Aceptar'  /></div>
		</form>
		<img class="back" src='<?= ADMINISTRATOR_IMAGE_PATH . 'back.png'; ?>' id="back" width = '64' height = '52' />
	</div>
</div>

<?php
	
	FunctionsHelper::includeBottom();
?>